package com.ruoyi.web.controller.thisstock;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ThisStock;
import com.ruoyi.service.IThisStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 本厂库存Controller
 *
 * @author ruoyi
 * @date 2020-07-06
 */
@Controller
@RequestMapping("/ruoyi/stock")
public class ThisStockController extends BaseController {
    private String prefix = "ruoyi/stock";

    @Autowired
    private IThisStockService thisStockService;

    @RequiresPermissions("ruoyi:stock:view")
    @GetMapping()
    public String stock() {
        return prefix + "/stock";
    }

    /**
     * 查询本厂库存列表
     */
    @RequiresPermissions("ruoyi:stock:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Long sid) {
        startPage();
        List<ThisStock> list = thisStockService.selectThisStockLists(sid);
        return getDataTable(list);
    }

    /**
     * 查询改项目下所有的材料类型
     */
    @RequiresPermissions("ruoyi:stock:list")
    @PostMapping("/like/{idss}")
    @ResponseBody
    public List<ThisStock> like(@PathVariable("idss") Long sid) {
        List<ThisStock> list = thisStockService.selectThisStockLists(sid);
//        list.stream().forEach(item->{
//            System.out.println();
//        });
        return list;
    }

    /**
     * 导出本厂库存列表
     */
    @RequiresPermissions("ruoyi:stock:export")
    @Log(title = "本厂库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ThisStock thisStock) {
        List<ThisStock> list = thisStockService.selectall(thisStock);
        ExcelUtil<ThisStock> util = new ExcelUtil<ThisStock>(ThisStock.class);
        return util.exportExcel(list, "本厂库存");
    }

    /**
     * 新增本厂库存
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存本厂库存
     */
    @RequiresPermissions("ruoyi:stock:add")
    @Log(title = "本厂库存", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ThisStock thisStock) {
        return toAjax(thisStockService.insertThisStock(thisStock));
    }

    /**
     * 修改本厂库存
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ThisStock thisStock = thisStockService.selectThisStockById(id);
        mmap.put("thisStock", thisStock);
        return prefix + "/edit";
    }

    /**
     * 修改保存本厂库存
     */
    @RequiresPermissions("ruoyi:stock:edit")
    @Log(title = "本厂库存", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ThisStock thisStock) {
        return toAjax(thisStockService.updateThisStock(thisStock));
    }

    /**
     * 删除本厂库存
     */
    @RequiresPermissions("ruoyi:stock:remove")
    @Log(title = "本厂库存", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(thisStockService.deleteThisStockByIds(ids));
    }
}
